package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.Employer;
import org.zwy.gradetrack.service.EmployerService;

import java.util.List;

/**
 * 用人单位控制器
 */
@RestController
@RequestMapping("/api/employers")
public class EmployerController {

    @Autowired
    private EmployerService employerService;

    /**
     * 分页查询用人单位列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<Page<Employer>>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name) {
        Page<Employer> page = new Page<>(current, size);
        return ResponseEntity.ok(Result.success(employerService.pageEmployers(page, name)));
    }

    /**
     * 获取所有用人单位
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<List<Employer>>> list() {
        return ResponseEntity.ok(Result.success(employerService.list()));
    }

    /**
     * 创建用人单位
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> create(@RequestBody Employer employer) {
        try {
            return ResponseEntity.ok(Result.success(employerService.createEmployer(employer)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 更新用人单位
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> update(@PathVariable Long id, @RequestBody Employer employer) {
        try {
            employer.setId(id);
            return ResponseEntity.ok(Result.success(employerService.updateEmployer(employer)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 删除用人单位
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> delete(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(Result.success(employerService.deleteEmployer(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }

    /**
     * 获取用人单位详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public ResponseEntity<Result<Employer>> getById(@PathVariable Long id) {
        try {
            return ResponseEntity.ok(Result.success(employerService.getEmployerById(id)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(404, e.getMessage()));
        }
    }

    /**
     * 根据名称查询用人单位
     */
    @GetMapping("/search/name")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<List<Employer>>> getByName(@RequestParam String name) {
        return ResponseEntity.ok(Result.success(employerService.getEmployersByName(name)));
    }

    /**
     * 批量导入用人单位
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public ResponseEntity<Result<Boolean>> batchImport(@RequestBody List<Employer> employers) {
        try {
            return ResponseEntity.ok(Result.success(employerService.batchImport(employers)));
        } catch (RuntimeException e) {
            return ResponseEntity.ok(Result.error(400, e.getMessage()));
        }
    }
} 